#include "board.h"
#include "ui_board.h"
#include <QPainter>
#include <QFont>
#include <QLabel>
#include <QWidget>
#include <QAction>
#include <QMenu>
#include <QToolTip>
#include <QMouseEvent>
#include <QStandardItemModel>
#include <QMessageBox>
#include <QPushButton>
#include <QProcess>
#include <QDialog>
#include <QMenuBar>
#include <QSound>
#include <cmath>
#include<QDebug>

int p=1;
setting::~setting()
{
    delete ui;
}

void setting::paintEvent(QPaintEvent*)
{
    QPainter painter(this);
    double l=height()/29;
    //绘制边框
    //外框
    painter.setPen(Qt::black);
    painter.setBrush(Qt::white);
    painter.drawRect(l,l,l*27,l*27);
    //内框
    painter.setPen(Qt::black);
    painter.setBrush(Qt::white);
    painter.drawRect(l*2.5,l*2.5,l*24,l*24);
    //画格子
    painter.setPen(Qt::black);
    painter.setBrush(Qt::gray);
    //棋盘格
    for(int i=0;i<8;i++)
    {
        for(int i1=0;i1<8;i1++)
        {
            if((i+i1)%2==1)
            painter.drawRect(l*(2.5+3*i),l*(2.5+3*i1),l*3,l*3);
        }
    }
    double a=l/9*5;
    char b[2],c[2];
    b[0]='8';
    b[1]=c[1]='\0';
    c[0]='a';
    char*b1=b;
    char*c1=c;
    painter.translate(l,l);
    for(char i=1;i<=8;i++)
    {
        painter.drawText(QPoint(a,a/4*3+l*3*i),tr(b1));
        painter.drawText(QPoint(l*3*i-l/9,height()-5*a),tr(c1));
        b[0]--;
        c[0]++;
    }
}

//棋子图片与名称联系
QLabel *label[32];
QVector<QString> pic;
void setting::getPicName(){
    pic.append(":/pics/pic/WRook.png");
    pic.append(":/pics/pic/WKnigt.png");
    pic.append(":/pics/pic/WBishop.png");
    pic.append(":/pics/pic/WQueen.png");
    pic.append(":/pics/pic/WKing.png");
    pic.append(":/pics/pic/WPawn.png");
    pic.append(":/pics/pic/BRook.png");
    pic.append(":/pics/pic/BKnigt.png");
    pic.append(":/pics/pic/BBishop.png");
    pic.append(":/pics/pic/BQueen.png");
    pic.append(":/pics/pic/BKing.png");
    pic.append(":/pics/pic/BPawn.png");
}

setting::setting(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::setting)
{
    ui->setupUi(this);

    //设置界面大小
    this->setGeometry(500,100,1100,900);

    //棋子序号与图片关联
    QString name;
    getPicName();
    for (int i=0;i<32;i++){
        myChess[i].startChess(i);
        //白棋
        if (i<=4)
            name=pic.at(i);
        else
            if (i>=5&&i<=7)
            name=pic.at(7-i);
        else
            if (i>=8&&i<=15)
            name=pic.at(5);
        //黑棋
        else
            if (i>=16&&i<=20)
            name=pic.at(i-10);
        else
            if (i>=21&&i<=23)
            name=pic.at(29-i);
        else
            if (i>=24)
            name=pic.at(11);

        label[i]=new QLabel(this); //stick chess pieces onto label
        label[i]->resize(height()/29*3,height()/29*3);
        label[i]->setPixmap(QPixmap(name));
        label[i]->setFixedSize(height()/29*2.5,height()/29*3);
        label[i]->setScaledContents(true);
        label[i]->setGeometry((myChess[i].x)*height()/29*3+height()/29*2.8,(myChess[i].y)*height()/29*3+height()/29*2.4,height()/29*3,height()/29*3);
        update();
    }
}

//判断该格中棋子类型
int setting::getNum(int xp, int yp){
    for (int i=0;i<32;i++)
        if (myChess[i].x==xp&&myChess[i].y==yp)
            return i;
    return -1;
}

//判断当前格子中是否有棋子
bool setting::exist(int xp, int yp){
    for (int i=0;i<32;i++)
        if (myChess[i].x==xp&&myChess[i].y==yp)
            return true;
    return false;
}

bool setting::middleChess(int n, int xp, int yp)
{
    //同列
    if (myChess[n].x==xp){
        if (yp>myChess[n].y){
            for (int i=myChess[n].y+1;i<yp;i++)
                if (exist(xp,i))
                    return true;
        }
        else {
            for (int i=yp+1;i<myChess[n].y;i++)
                if (exist(xp,i))
                    return true;
        }
    }
    //同行
    else if (myChess[n].y==yp)
    {
        if (xp>myChess[n].x){
            for (int i=myChess[n].x+1;i<xp;i++)
                if (exist(i,yp))
                    return true;
        }
        else {
            for (int i=xp+1;i<myChess[n].x;i++)
                if (exist(i,yp))
                    return true;
        }
    }
    //不同行也不同列
    else
    {
        if (xp>myChess[n].x){
            if (yp>myChess[n].y){
                int j=myChess[n].y+1;
                for (int i=myChess[n].x+1;i<xp;i++)
                    if (exist(i,j))
                        return true;
                    else
                        j++;
            }
            else{
                int j=myChess[n].y-1;
                for (int i=myChess[n].x+1;i<xp;i++)
                    if (exist(i,j))
                        return true;
                    else
                        j--;
            }
        }
        else{
            if (yp>myChess[n].y){
                int j=myChess[n].y+1;
                for (int i=myChess[n].x-1;i>xp;i--)
                    if (exist(i,j))
                        return true;
                    else
                        j++;
            }
            else{
                int j=myChess[n].y-1;
                for (int i=myChess[n].x-1;i>xp;i--)
                    if (exist(i,j))
                        return true;
                    else
                        j--;
            }
        }
    }
    return false;
}

//判断棋子颜色是否相同
bool setting::sameColor(int n1, int n2){
    if (myChess[n1].white==myChess[n2].white)
        return true;
    return false;
}

//车走动是否合法
bool setting::moveRook(int n1, int n2, int xp, int yp){
    if (xp==myChess[n1].x&&yp==myChess[n1].y)
        return false;
    if (exist(xp,yp)&&sameColor(n1,n2))
        return false;
    if (middleChess(n1,xp,yp))
        return false;
    if (xp!=myChess[n1].x&&yp!=myChess[n1].y)
        return false;
    return true;
}

//马走动是否合法
bool setting::moveKnight(int n1, int n2, int xp, int yp){
    if (xp==myChess[n1].x&&yp==myChess[n1].y)
        return false;
    if (exist(xp,yp)&&sameColor(n1,n2))
        return false;
    if (abs(xp-myChess[n1].x)==1&&abs(yp-myChess[n1].y)==2)
        return true;
    else if (abs(xp-myChess[n1].x)==2&&abs(yp-myChess[n1].y)==1)
        return true;
    return false;
}

//象走动是否合法
bool setting::moveBishop(int n1, int n2, int xp, int yp){
    if (xp==myChess[n1].x&&yp==myChess[n1].y)
        return false;
    if (exist(xp,yp)&&sameColor(n1,n2))
        return false;
    if (middleChess(n1,xp,yp))
        return false;
    if (abs(xp-myChess[n1].x)==abs(yp-myChess[n1].y))
        return true;
    return false;
}

//后走动是否合法
bool setting::moveQueen(int n1, int n2, int xp, int yp){
    if (xp==myChess[n1].x&&yp==myChess[n1].y)
        return false;
    if (exist(xp,yp)&&sameColor(n1,n2))
        return false;
    if (middleChess(n1,xp,yp))
        return false;
    if (xp==myChess[n1].x||yp==myChess[n1].y)
        return true;
    else if (abs(xp-myChess[n1].x)==abs(yp-myChess[n1].y))
        return true;
    return false;
}


bool whiteCastled=false,blackCastled=false;

bool setting::castling(int n, int xp,int yp)
{
    //白方易位
    if (myChess[n].white){
        //长易位
        if (xp>myChess[n].x){
            if (!myChess[7].moved&&!myChess[n].moved){
                if (!middleChess(n,myChess[7].x,myChess[7].y)){
                    for (int i=16;i<32;i++){
                        if (threat(i,myChess[n].x,myChess[n].y))
                            return false;
                        if (threat(i,myChess[n].x+1,myChess[n].y))
                            return false;
                        if (threat(i,myChess[n].x+2,myChess[n].y))
                            return false;
                    }
                }
            }
        }
        else{
            //长易位
            if (!myChess[0].moved&&!myChess[n].moved){
                if (!middleChess(n,myChess[0].x,myChess[0].y)){
                    for (int i=16;i<32;i++){
                        if (threat(i,myChess[n].x,myChess[n].y))
                            return false;
                        if (threat(i,myChess[n].x-1,myChess[n].y))
                            return false;
                        if (threat(i,myChess[n].x-2,myChess[n].y))
                            return false;
                    }
                }
            }
        }
        whiteCastled=true;
    }
    //黑方易位
    else{
        if (xp>myChess[n].x)
        {
            //短易位
            if (!myChess[23].moved&&!myChess[n].moved){
                if (!middleChess(n,myChess[23].x,myChess[23].y)){
                    for (int i=0;i<16;i++){
                        if (threat(i,myChess[n].x,myChess[n].y))
                            return false;
                        if (threat(i,myChess[n].x+1,myChess[n].y))
                            return false;
                        if (threat(i,myChess[n].x+2,myChess[n].y))
                            return false;
                    }
                }
            }
        }
        else{
            //长易位
            if (!myChess[16].moved&&!myChess[n].moved){
                if (!middleChess(n,myChess[16].x,myChess[16].y)){
                    for (int i=0;i<16;i++){
                        if (threat(i,myChess[n].x,myChess[n].y))
                            return false;
                        if (threat(i,myChess[n].x-1,myChess[n].y))
                            return false;
                        if (threat(i,myChess[n].x-2,myChess[n].y))
                            return false;
                    }
                }
            }
        }
        blackCastled=true;
    }
    return true;
}

//王走动是否合法
bool setting::moveKing(int n1, int n2, int xp, int yp){
    if (xp==myChess[n1].x&&yp==myChess[n1].y)
        return false;
    if (exist(xp,yp)&&sameColor(n1,n2))
        return false;
    //斜走
    if (abs(xp-myChess[n1].x)==1&&abs(yp-myChess[n1].y)==1)
        return true;
    //竖走
    if (xp==myChess[n1].x&&abs(yp-myChess[n1].y)==1)
        return true;
    //横走
    if (abs(xp-myChess[n1].x)==1&&yp==myChess[n1].y)
        return true;
    //易位
    if (yp==myChess[n1].y&&abs(xp-myChess[n1].x)==2)
    {
        if (myChess[n1].white&&!whiteCastled)
            return castling(n1,xp,yp);
        else if (!myChess[n1].white&&!blackCastled)
            return castling(n1,xp,yp);
        else
            return false;
    }
    return false;
}

int white=16,black=16;

//吃过路兵
bool setting::enPassant(int n1, int xp, int yp){
    int n2=-1;
    if (myChess[n1].white){
        n2=getNum(xp,yp+1);
        if (n2>=24&&n2<32){
            if (myChess[n2].y==3&&myChess[n2].moved==1){
                myChess[n2].dead=true;
                myChess[n2].x=-1;
                myChess[n2].y=-1;
                label[n2]->clear();
                black--;
                return true;
            }
            else
                return false;
        }
        else
            return false;
    }
    else{
        n2=getNum(xp,yp-1);
        if (n2>=8&&n2<16){
            if (myChess[n2].y==4&&myChess[n2].moved==1){
                myChess[n2].dead=true;
                myChess[n2].x=-1;
                myChess[n2].y=-1;
                label[n2]->clear();
                white--;
                return true;
            }
            else
                return false;
        }
        else
            return false;
    }
}

//兵的移动
bool setting::movePawn(int n1, int n2, int xp, int yp){
    if (xp==myChess[n1].x&&yp==myChess[n1].y)
        return false;
    if (n2!=-1&&sameColor(n1,n2))
        return false;
    if (n2!=-1&&myChess[n1].x==xp&&abs(myChess[n1].y-yp)==1)
        return false;
    if (xp==myChess[n1].x){
        if (myChess[n1].white){
            if ((myChess[n1].y-yp)==2&&!myChess[n1].moved)
                return true;
            else if (myChess[n1].y-yp==1)
                return true;
        }
        else {
            if ((yp-myChess[n1].y)==2&&!myChess[n1].moved)
                return true;
            else if (yp-myChess[n1].y==1)
                return true;
        }
    }
    else if (abs(xp-myChess[n1].x)==1){
        if (myChess[n1].white&&(myChess[n1].y-yp==1)){
            if (n2!=-1)
                return true;
            else if (enPassant(n1,xp,yp))
                return true;
        }
        else if (!myChess[n1].white&&(yp-myChess[n1].y==1)){
            if (n2!=-1)
                return true;
            else if (enPassant(n1,xp,yp))
                return true;
        }
    }
    return false;
}

bool setting::threat(int n, int xk, int yk)
{
    if (myChess[n].dead)
        return false;
    if (myChess[n].chessType==0){ //rook
        if (myChess[n].x==xk||myChess[n].y==yk){
            if (middleChess(n,xk,yk))
                return false;
            else
                return true;
         }
         else
            return false;
    }
    else if (myChess[n].chessType==1){ //knight
        if (abs(xk-myChess[n].x)==1&&abs(yk-myChess[n].y)==2)
            return true;
        else if (abs(xk-myChess[n].x)==2&&abs(yk-myChess[n].y)==1)
            return true;
        else
            return false;
    }
    else if (myChess[n].chessType==2){ //bishop
        if (abs(xk-myChess[n].x)==abs(yk-myChess[n].y)){
            if (middleChess(n,xk,yk))
                return false;
            else
                return true;
        }
        else
            return false;
    }
    else if (myChess[n].chessType==3){ //queen
        if (myChess[n].x==xk||myChess[n].y==yk){
            if (middleChess(n,xk,yk))
                return false;
            else
                return true;
        }
        else if (abs(xk-myChess[n].x)==abs(yk-myChess[n].y)){
            if (middleChess(n,xk,yk))
                return false;
            else
                return true;
        }
        else
            return false;
    }
    else if (myChess[n].chessType==4){ //king
        if (abs(myChess[n].x-xk)==1&&abs(myChess[n].y-yk)==1)
            return true;
        else if (myChess[n].x==xk&&abs(myChess[n].y-yk)==1)
            return true;
        else if (abs(myChess[n].x-xk)==1&&myChess[n].y==yk)
            return true;
        else
            return false;
    }
    else if (myChess[n].chessType==5)
    { //pawn
        if (myChess[n].white){
            if ((myChess[n].y-yk)==1&&abs(myChess[n].x-xk)==1)
                return true;
            else
                return false;
        }
        else{
            if ((yk-myChess[n].y)==1&&abs(xk-myChess[n].x)==1)
                return true;
            else
                return false;
        }
    }
}

int whiteTurn=1;
bool isthreatened=false;

//判断是否有棋可走
bool setting::legalMove(){
    int os=0,es=0,oe=0,ee=0;
    if (whiteTurn==1){
        os=0;
        oe=16;
        es=16;
        ee=32;
    }
    else{
        os=16;
        oe=32;
        es=0;
        ee=16;
    }
    for (int i=os;i<oe;i++){
        int xt=myChess[i].x;
        int yt=myChess[i].y;
        int nt=-1,j=0,m=0;
        if (myChess[i].chessType==stone::rook)
        {
            for (;j<8;j++){
                nt=getNum(j,yt);
                if (move(i,nt,j,yt))
                    return true;
            }
            for (;m<8;m++){
                nt=getNum(xt,m);
                if (move(i,nt,xt,m))
                    return true;
            }
        }
        else if(myChess[i].chessType==stone::knight){
            int xx[8]={xt-1,xt-1,xt-2,xt-2,xt+1,xt+1,xt+2,xt+2};
            int yy[8]={yt-2,yt+2,yt-1,yt+1,yt-2,yt+2,yt-1,yt+1};
            for (j=0;j<8;j++){
                nt=getNum(xx[j],yy[j]);
                if (move(i,nt,xx[j],yy[j]))
                    return true;
            }
        }
        else if (myChess[i].chessType==stone::bishop){
            for (j=xt+1;j<8;j++){
                m=yt+j-xt;
                nt=getNum(j,m);
                if (move(i,nt,j,m))
                    return true;
            }
            for (j=xt+1;j<8;j++){
                m=yt-j+xt;
                nt=getNum(j,m);
                if (move(i,nt,j,m))
                    return true;
            }
            for (j=xt-1;j>=0;j--){
                m=yt+j-xt;
                nt=getNum(j,m);
                if (move(i,nt,j,m))
                    return true;
            }
            for (j=xt-1;j>=8;j--){
                m=yt-j+xt;
                nt=getNum(j,m);
                if (move(i,nt,j,m))
                    return true;
            }
        }
        else if (myChess[i].chessType==stone::queen){
            //横
            for (j=0;j<8;j++){
                nt=getNum(j,yt);
                if (move(i,nt,j,yt))
                    return true;
            }
            //竖
            for (m=0;m<8;m++){
                nt=getNum(xt,m);
                if (move(i,nt,xt,m))
                    return true;
            }
            //斜线
            for (j=xt+1;j<8;j++){
                m=yt+j-xt;
                nt=getNum(j,m);
                if (move(i,nt,j,m))
                    return true;
            }
            for (j=xt+1;j<8;j++){
                m=yt-j+xt;
                nt=getNum(j,m);
                if (move(i,nt,j,m))
                    return true;
            }
            for (j=xt-1;j>=0;j--){
                m=yt+j-xt;
                nt=getNum(j,m);
                if (move(i,nt,j,m))
                    return true;
            }
            for (j=xt-1;j>=8;j--){
                m=yt-j+xt;
                nt=getNum(j,m);
                if (move(i,nt,j,m))
                    return true;
            }
        }
        else if (myChess[i].chessType==stone::king)
        {
            p=1;
            qDebug() << "测试王" << endl;
            int xx[8]={xt-1,xt,xt+1,xt+1,xt+1,xt,xt-1,xt-1};
            int yy[8]={yt-1,yt-1,yt-1,yt,yt+1,yt+1,yt+1,yt};
            for (j=0;j<8;j++){
                nt=getNum(xx[j],yy[j]);
                if(xx[j]>=0&&xx[j]<=7&&yy[j]>=0&&yy[j]<=7)
                {
                  if (move(i,nt,xx[j],yy[j]))
                  {
                     p=0;
                     qDebug() << "可移动" << endl;
                     return true;
                  }
                }
            }
        }
        else if (myChess[i].chessType==stone::pawn){
            if (whiteTurn==1)
                m=yt-1;
            else
                m=yt+1;
            nt=getNum(xt,m);
            if (move(i,nt,xt,m))
                return true;
            j=xt-1;
            if (move(i,nt,j,m))
                return true;
            j=xt+1;
            if (move(i,nt,j,m))
                return true;
        }
    }
    return false;
}

//判断将杀
bool setting::checkmate()
{
    if (whiteTurn==1){
        for (int i=16;i<32;i++)
        {
            if (threat(i,myChess[4].x,myChess[4].y))
            {
                isthreatened=true;
                break;
            }
            else
                isthreatened=false;
        }
        if (isthreatened)
        {
            legalMove();
            if (p==1)
            {
                return true;}
            else
                return false;
        }
        else
            return false;
    }
    else{
        qDebug() << "1.1" << endl;
        for (int i=0;i<16;i++){
            if (threat(i,myChess[20].x,myChess[20].y))
            {
                isthreatened=true;
                break;
            }
            else
                isthreatened=false;
        }
        if (isthreatened)
        {
            legalMove();
            if (p==1)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        else
            return false;
    }
}

bool setting::stalemate(){
    if (whiteTurn==1){
        for (int i=16;i<32;i++){
            if (threat(i,myChess[4].x,myChess[4].y)){
                isthreatened=true;
                break;
            }
            else
                isthreatened=false;
        }
        if (!isthreatened){
            if (!legalMove())
                return true;
            else
                return false;
        }
        else
            return false;
    }
    else{
        for (int i=0;i<16;i++){
            if (threat(i,myChess[20].x,myChess[20].y)){
                isthreatened=true;
                break;
            }
            else
                isthreatened=false;
        }
        if (!isthreatened){
            if (!legalMove())
                return true;
            else
                return false;
        }
        else
            return false;
    }
}

bool setting::draw(){
    if (white==1&&black==2)
        return true;
    if (white==2&&black==1)
        return true;
    if (white==1&&black==1)
        return true;
    return false;
}

bool setting::move(int n1, int n2, int xp, int yp){
    //if it's the right side to move
    if (n1>=0&&n1<=15){
        if (whiteTurn==1) ;
        else
            return false;
    }
    else if (n1>=16&&n1<=31){
        if (whiteTurn==1)
            return false;
    }
    else
        return false;
    if (xp<0||xp>7||yp<0||yp>7)
        return false;

    //if it moves according to rules
    switch(myChess[n1].chessType){
        case 0:
            if(moveRook(n1,n2,xp,yp));
            else return false;
            break;
        case 1:
            if(moveKnight(n1,n2,xp,yp));
            else return false;
            break;
        case 2:
            if(moveBishop(n1,n2,xp,yp));
            else return false;
            break;
        case 3:
            if(moveQueen(n1,n2,xp,yp));
            else return false;
            break;
        case 4:
            if(moveKing(n1,n2,xp,yp));
            else return false;
            break;
        case 5:
            if(movePawn(n1,n2,xp,yp));
            else return false;
            break;
        default:
            return false;
            break;
    }

    //suppose it moves
    int xt=myChess[n1].x,yt=myChess[n1].y;
    myChess[n1].x=xp;
    myChess[n1].y=yp;
    if (n2!=-1)
        myChess[n2].dead=true;
    if (myChess[n1].white){ //if white would be threatened after it moves
        for (int i=16;i<32;i++){
            if (!myChess[i].dead&&threat(i,myChess[4].x,myChess[4].y)){
                isthreatened=true;
                break;
            }
            else isthreatened=false;
        }
    }
    else { //if black would be threatened after it moves
        for (int i=0;i<16;i++){
            if (!myChess[i].dead&&threat(i,myChess[20].x,myChess[20].y)){
                isthreatened=true;
                break;
            }
            else isthreatened=false;
        }
    }
    myChess[n1].x=xt;
    myChess[n1].y=yt;
    if (n2!=-1)
        myChess[n2].dead=false;
    if (isthreatened)
        return false;
    else
        return true;
}

//移动棋子
void setting::moveChess(int n1,int n2,int xp, int yp)
{
    //吃子
    if (n2!=-1){
       myChess[n2].dead=true;
       myChess[n2].x=-1;
       myChess[n2].y=-1;
       label[n2]->clear();
       if (myChess[n2].white)
           white--;
       else
           black--;
    }
    int xk=myChess[n1].x-xp;
    myChess[n1].x=xp;
    myChess[n1].y=yp;
    label[n1]->move((xp)*height()/29*3+height()/29*2.8,(yp)*height()/29*3+height()/29*2.4);
    //易位
    if (myChess[n1].chessType==4&&yp==myChess[n1].y&&abs(xk)==2)
    {
        qDebug() << "1" << endl;
        if (myChess[n1].white){
            qDebug() << "2" << endl;
            if (xk<0){
                myChess[7].x-=2;
                qDebug() << "白短易位" << endl;
                label[7]->move((myChess[7].x)*height()/29*3+height()/29*2.8,(myChess[7].y)*height()/29*3+height()/29*2.4);
                update();
            }
            else{
                myChess[0].x+=3;
                qDebug() << "白长易位" << endl;
                label[0]->move((myChess[0].x)*height()/29*3+height()/29*2.8,(myChess[0].y)*height()/29*3+height()/29*2.4);
                update();
            }
        }
        else{
            if (xk<0){
                myChess[23].x-=2;
                qDebug() << "黑短易位" << endl;
                label[23]->move((myChess[23].x)*height()/29*3+height()/29*2.8,(myChess[23].y)*height()/29*3+height()/29*2.4);
            }
            else{ //long castling
                myChess[16].x+=3;
                qDebug() << "黑长易位" << endl;
                label[16]->move((myChess[16].x)*height()/29*3+height()/29*2.8,(myChess[16].y)*height()/29*3+height()/29*2.4);
            }
        }
    }


    //兵的底线升变
    if (myChess[n1].chessType==stone::pawn&&(yp==0||yp==7)){ //pawn promotion
        QMessageBox mess(QMessageBox::Information, tr("提示"), tr("兵已达到底线，可选择升变为："));
            QPushButton *buttonRook = (mess.addButton(tr("车"), QMessageBox::AcceptRole));
            QPushButton *buttonQueen = (mess.addButton(tr("后"), QMessageBox::YesRole));
            QPushButton *buttonBishop = (mess.addButton(tr("象"), QMessageBox::RejectRole));
            QPushButton *buttonKnight = (mess.addButton(tr("马"), QMessageBox::RejectRole));
        mess.exec();
        qDebug() << "兵升变" << endl;
        QSound::play(":/pics/sound/pawn.wav");
        label[n1]->clear();
        if (mess.clickedButton()==buttonRook){
            myChess[n1].chessType=stone::rook;
            if (myChess[n1].white)
                label[n1]->setPixmap(QPixmap(":/pics/pic/WRook.png"));
            else
                label[n1]->setPixmap(QPixmap(":/pics/pic/BRook.png"));
        }
        else if(mess.clickedButton()==buttonQueen){
            myChess[n1].chessType=stone::queen;
            if (myChess[n1].white)
                label[n1]->setPixmap(QPixmap(":/pics/pic/WQueen.png"));
            else
                label[n1]->setPixmap(QPixmap(":/pics/pic/BQueen.png"));
        }
        else if(mess.clickedButton()==buttonBishop){
            myChess[n1].chessType=stone::bishop;
            if (myChess[n1].white)
                label[n1]->setPixmap(QPixmap(":/pics/pic/WBishop.png"));
            else
                label[n1]->setPixmap(QPixmap(":/pics/pic/BBishop.png"));
        }
        else if(mess.clickedButton()==buttonKnight){
            myChess[n1].chessType=stone::knight;
            if (myChess[n1].white)
                label[n1]->setPixmap(QPixmap(":/pics/pic/WKnigt.png"));
            else
                label[n1]->setPixmap(QPixmap(":/pics/pic/BKnigt.png"));
        }
        label[n1]->resize(height()/29*3,height()/29*3);
        label[n1]->setFixedSize(height()/29*2.5,height()/29*3);
        label[n1]->setScaledContents(true);
        label[n1]->setGeometry((myChess[n1].x)*height()/29*3+height()/29*2.8,(myChess[n1].y)*height()/29*3+height()/29*2.4,height()/29*3,height()/29*3);
    }
    update();
}



void setting::mouseMoveEvent(QMouseEvent *e){
    QToolTip::showText(e->globalPos(), QString("( %1, %2)").arg(e->x()).arg(e->y()));
    update();
}


QMessageBox::StandardButton result;
void setting::mousePressEvent(QMouseEvent *e){
    static int move_flag=0,n1=0,n2=0;
    int xp=-1,yp=-1;
    //获取点击坐标
    for (int i=0;i<8;i++){
        for (int j=0;j<9;j++){
            if (pow(e->x()-(i+1)*height()/29*3-height()/29*2.8,2)+pow(e->y()-(j)*height()/29*3-height()/29*2.4,2)<pow(height()/29*3,2)){
                xp=i;
                yp=j;
                break;
            }
        }
    }
    if (xp>-1&&yp>-1){
        switch (move_flag) {
        case 0:{
            n1=getNum(xp,yp);
            move_flag++;
        }
            break;
        case 1:{
            n2=getNum(xp,yp);
            move_flag++;
        }
            break;
        }
        if (move_flag==2)
        {
            move_flag=0;
            if (move(n1,n2,xp,yp)) {
                qDebug() << "移动" << endl;
                QSound::play(":/pics/sound/movechess.wav");
                moveChess(n1,n2,xp,yp);
                myChess[n1].moved++;
                whiteTurn*=-1;
                QFont font ( 0, 20, 75); //第一个属性是字体（微软雅黑），第二个是大小，第三个是加粗（权重是75）
                ui->label1->move(890,100);
                if(whiteTurn==1)
                    ui->label1->setText("白棋执子");
                else
                    ui->label1->setText("黑棋执子");
                ui-> label1->setFont(font);
                checkmate();
                if (checkmate()||stalemate()||draw())
                {
                    qDebug() << "判断" << endl;
                    QSound::play(":/pics/sound/victory.wav");
                    if (checkmate())
                    {
                        if (whiteTurn==1)
                            result=QMessageBox::information(NULL,"祝贺！","黑方获胜",QMessageBox::Retry|QMessageBox::Ok);
                        else
                            result=QMessageBox::information(NULL,"祝贺！","白方获胜",QMessageBox::Retry|QMessageBox::Ok);
                    }
                    else if (stalemate()){
                        result=QMessageBox::information(NULL,"提示！","Stalemate!",QMessageBox::Retry|QMessageBox::Ok);
                    }
                    else if (draw()){
                        result=QMessageBox::information(NULL,"提示！","Draw!",QMessageBox::Retry|QMessageBox::Ok);
                    }
                    if (result==QMessageBox::Ok){
                        this->close();
                        QApplication::exit();
                    }
                    else{
                        QApplication::quit();
                        QProcess::startDetached(QApplication::applicationFilePath(),QStringList());
                    }
                }
            }
            else if (isthreatened){
                QSound::play(":/pics/sound/warning.wav");
                QMessageBox::information(NULL,"警告","王正在被将军",QMessageBox::Retry|QMessageBox::Ok,QMessageBox::Retry);
            }
            else{
                QSound::play(":/pics/sound/warning.wav");
                QMessageBox::information(NULL,"警告","非法移动，请重试！",QMessageBox::Retry|QMessageBox::Ok,QMessageBox::Retry);
            }
        }
    }
}


